package com.lizhi.getip.controller;

import com.lizhi.getip.utils.AddressUtils;
import com.lizhi.getip.utils.IpUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;

@RestController
public class GetIPController {

    /**
     * 获取ip和省份城市
     *
     * @param request
     * @return
     */
    @GetMapping("getIp")
    public String getIp(HttpServletRequest request) {

        String ipStr = IpUtils.getIpAddr(request);
        System.out.println("ip地址 = " + ipStr);

        String ipAddress = AddressUtils.getRealAddressByIP(ipStr);
        System.out.println("ip所属区域 = " + ipAddress);

        return "ip地址=" + ipStr + " ip所属区域=" + ipAddress;

    }

    /**
     * 根据ip获取所有未处理信息
     *
     * {
     *     "全部信息": "{\"ip\":\"116.52.81.76\",\"pro\":\"云南省\",\"proCode\":\"530000\",\"city\":\"昆明市\",\"cityCode\":\"530100\",\"region\":\"\",\"regionCode\":\"0\",\"addr\":\"云南省昆明市 电信\",\"regionNames\":\"\",\"err\":\"\"}",
     *     "省份城市": "云南省 昆明市"
     * }
     * 
     * * @param request
     * @return
     */
    @GetMapping("getAllInfoByIp")
    public Map<String, Object> getAllInfoByIp(HttpServletRequest request) {

        String ipStr = IpUtils.getIpAddr(request);
        System.out.println("ip地址 = " + ipStr);

        return AddressUtils.getAllInfoByIp(ipStr);
    }
}
